{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "\n",
    "def loadDataSet(fileName):      #general function to parse tab -delimited floats\n",
    "    dataMat = []                #assume last column is target value\n",
    "    fr = open(fileName)\n",
    "    for line in fr.readlines():\n",
    "        curLine = line.strip().split('\\t')\n",
    "        fltLine = list(map(float,curLine)) #map all elements to float()\n",
    "        dataMat.append(fltLine)\n",
    "    return dataMat\n",
    "\n",
    "def binSplitDataSet(dataSet, feature, value):\n",
    "    mat0 = dataSet[nonzero(dataSet[:,feature] > value)[0],:]\n",
    "    mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:]\n",
    "    return mat0,mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.,  0.,  0.,  0.],\n",
       "        [ 0.,  1.,  0.,  0.],\n",
       "        [ 0.,  0.,  1.,  0.],\n",
       "        [ 0.,  0.,  0.,  1.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testMat = mat(eye(4))\n",
    "testMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.,  0.,  0.,  0.],\n",
       "        [ 0.,  0.,  1.,  0.],\n",
       "        [ 0.,  0.,  0.,  1.]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testMat[nonzero(testMat[:, 1] < 0.5)[0],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 0.,  1.,  0.,  0.]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat0,mat1 = binSplitDataSet(testMat, 1, 0.5)\n",
    "mat0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.,  0.,  0.,  0.],\n",
       "        [ 0.,  0.,  1.,  0.],\n",
       "        [ 0.,  0.,  0.,  1.]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  3.60980000e-02,   1.55096000e-01],\n",
       "        [  9.93349000e-01,   1.07755300e+00],\n",
       "        [  5.30897000e-01,   8.93462000e-01],\n",
       "        [  7.12386000e-01,   5.64858000e-01],\n",
       "        [  3.43554000e-01,  -3.71700000e-01],\n",
       "        [  9.80160000e-02,  -3.32760000e-01],\n",
       "        [  6.91115000e-01,   8.34391000e-01],\n",
       "        [  9.13580000e-02,   9.99350000e-02],\n",
       "        [  7.27098000e-01,   1.00056700e+00],\n",
       "        [  9.51949000e-01,   9.45255000e-01],\n",
       "        [  7.68596000e-01,   7.60219000e-01],\n",
       "        [  5.41314000e-01,   8.93748000e-01],\n",
       "        [  1.46366000e-01,   3.42830000e-02],\n",
       "        [  6.73195000e-01,   9.15077000e-01],\n",
       "        [  1.83510000e-01,   1.84843000e-01],\n",
       "        [  3.39563000e-01,   2.06783000e-01],\n",
       "        [  5.17921000e-01,   1.49358600e+00],\n",
       "        [  7.03755000e-01,   1.10167800e+00],\n",
       "        [  8.30700000e-03,   6.99760000e-02],\n",
       "        [  2.43909000e-01,  -2.94670000e-02],\n",
       "        [  3.06964000e-01,  -1.77321000e-01],\n",
       "        [  3.64920000e-02,   4.08155000e-01],\n",
       "        [  2.95511000e-01,   2.88200000e-03],\n",
       "        [  8.37522000e-01,   1.22937300e+00],\n",
       "        [  2.02054000e-01,  -8.77440000e-02],\n",
       "        [  9.19384000e-01,   1.02988900e+00],\n",
       "        [  3.77201000e-01,  -2.43550000e-01],\n",
       "        [  8.14825000e-01,   1.09520600e+00],\n",
       "        [  6.11270000e-01,   9.82036000e-01],\n",
       "        [  7.22430000e-02,  -4.20983000e-01],\n",
       "        [  4.10230000e-01,   3.31722000e-01],\n",
       "        [  8.69077000e-01,   1.11482500e+00],\n",
       "        [  6.20599000e-01,   1.33442100e+00],\n",
       "        [  1.01149000e-01,   6.88340000e-02],\n",
       "        [  8.20802000e-01,   1.32590700e+00],\n",
       "        [  5.20044000e-01,   9.61983000e-01],\n",
       "        [  4.88130000e-01,  -9.77910000e-02],\n",
       "        [  8.19823000e-01,   8.35264000e-01],\n",
       "        [  9.75022000e-01,   6.73579000e-01],\n",
       "        [  9.53112000e-01,   1.06469000e+00],\n",
       "        [  4.75976000e-01,  -1.63707000e-01],\n",
       "        [  2.73147000e-01,  -4.55219000e-01],\n",
       "        [  8.04586000e-01,   9.24033000e-01],\n",
       "        [  7.47950000e-02,  -3.49692000e-01],\n",
       "        [  6.25336000e-01,   6.23696000e-01],\n",
       "        [  6.56218000e-01,   9.58506000e-01],\n",
       "        [  8.34078000e-01,   1.01058000e+00],\n",
       "        [  7.81930000e-01,   1.07448800e+00],\n",
       "        [  9.84900000e-03,   5.65940000e-02],\n",
       "        [  3.02217000e-01,  -1.48650000e-01],\n",
       "        [  6.78287000e-01,   9.07727000e-01],\n",
       "        [  1.80506000e-01,   1.03676000e-01],\n",
       "        [  1.93641000e-01,  -3.27589000e-01],\n",
       "        [  3.43479000e-01,   1.75264000e-01],\n",
       "        [  1.45809000e-01,   1.36979000e-01],\n",
       "        [  9.96757000e-01,   1.03553300e+00],\n",
       "        [  5.90210000e-01,   1.33666100e+00],\n",
       "        [  2.38070000e-01,  -3.58459000e-01],\n",
       "        [  5.61362000e-01,   1.07052900e+00],\n",
       "        [  3.77597000e-01,   8.85050000e-02],\n",
       "        [  9.91420000e-02,   2.52800000e-02],\n",
       "        [  5.39558000e-01,   1.05384600e+00],\n",
       "        [  7.90240000e-01,   5.33214000e-01],\n",
       "        [  2.42204000e-01,   2.09359000e-01],\n",
       "        [  1.52324000e-01,   1.32858000e-01],\n",
       "        [  2.52649000e-01,  -5.56130000e-02],\n",
       "        [  8.95930000e-01,   1.07727500e+00],\n",
       "        [  1.33300000e-01,  -2.23143000e-01],\n",
       "        [  5.59763000e-01,   1.25315100e+00],\n",
       "        [  6.43665000e-01,   1.02424100e+00],\n",
       "        [  8.77241000e-01,   7.97005000e-01],\n",
       "        [  6.13765000e-01,   1.62109100e+00],\n",
       "        [  6.45762000e-01,   1.02688600e+00],\n",
       "        [  6.51376000e-01,   1.31538400e+00],\n",
       "        [  6.97718000e-01,   1.21243400e+00],\n",
       "        [  7.42527000e-01,   1.08705600e+00],\n",
       "        [  9.01056000e-01,   1.05590000e+00],\n",
       "        [  3.62314000e-01,  -5.56464000e-01],\n",
       "        [  9.48268000e-01,   6.31862000e-01],\n",
       "        [  2.34000000e-04,   6.09030000e-02],\n",
       "        [  7.50078000e-01,   9.06291000e-01],\n",
       "        [  3.25412000e-01,  -2.19245000e-01],\n",
       "        [  7.26828000e-01,   1.01711200e+00],\n",
       "        [  3.48013000e-01,   4.89390000e-02],\n",
       "        [  4.58121000e-01,  -6.14560000e-02],\n",
       "        [  2.80738000e-01,  -2.28880000e-01],\n",
       "        [  5.67704000e-01,   9.69058000e-01],\n",
       "        [  7.50918000e-01,   7.48104000e-01],\n",
       "        [  5.75805000e-01,   8.99090000e-01],\n",
       "        [  5.07940000e-01,   1.10726500e+00],\n",
       "        [  7.17690000e-02,  -1.10946000e-01],\n",
       "        [  5.53520000e-01,   1.39127300e+00],\n",
       "        [  4.01152000e-01,  -1.21640000e-01],\n",
       "        [  4.06649000e-01,  -3.66317000e-01],\n",
       "        [  6.52121000e-01,   1.00434600e+00],\n",
       "        [  3.47837000e-01,  -1.53405000e-01],\n",
       "        [  8.19310000e-02,  -2.69756000e-01],\n",
       "        [  8.21648000e-01,   1.28089500e+00],\n",
       "        [  4.80140000e-02,   6.44960000e-02],\n",
       "        [  1.30962000e-01,   1.84241000e-01],\n",
       "        [  7.73422000e-01,   1.12594300e+00],\n",
       "        [  7.89625000e-01,   5.52614000e-01],\n",
       "        [  9.69940000e-02,   2.27167000e-01],\n",
       "        [  6.25791000e-01,   1.24473100e+00],\n",
       "        [  5.89575000e-01,   1.18581200e+00],\n",
       "        [  3.23181000e-01,   1.80811000e-01],\n",
       "        [  8.22443000e-01,   1.08664800e+00],\n",
       "        [  3.60323000e-01,  -2.04830000e-01],\n",
       "        [  9.50153000e-01,   1.02290600e+00],\n",
       "        [  5.27505000e-01,   8.79560000e-01],\n",
       "        [  8.60049000e-01,   7.17490000e-01],\n",
       "        [  7.04400000e-03,   9.41500000e-02],\n",
       "        [  4.38367000e-01,   3.40140000e-02],\n",
       "        [  5.74573000e-01,   1.06613000e+00],\n",
       "        [  5.36689000e-01,   8.67284000e-01],\n",
       "        [  7.82167000e-01,   8.86049000e-01],\n",
       "        [  9.89888000e-01,   7.44207000e-01],\n",
       "        [  7.61474000e-01,   1.05826200e+00],\n",
       "        [  9.85425000e-01,   1.22794600e+00],\n",
       "        [  1.32543000e-01,  -3.29372000e-01],\n",
       "        [  3.46986000e-01,  -1.50389000e-01],\n",
       "        [  7.68784000e-01,   8.99705000e-01],\n",
       "        [  8.48921000e-01,   1.17095900e+00],\n",
       "        [  4.49280000e-01,   6.90980000e-02],\n",
       "        [  6.61720000e-02,   5.24390000e-02],\n",
       "        [  8.13719000e-01,   7.06601000e-01],\n",
       "        [  6.61923000e-01,   7.67040000e-01],\n",
       "        [  5.29491000e-01,   1.02220600e+00],\n",
       "        [  8.46455000e-01,   7.20030000e-01],\n",
       "        [  4.48656000e-01,   2.69740000e-02],\n",
       "        [  7.95072000e-01,   9.65721000e-01],\n",
       "        [  1.18156000e-01,  -7.74090000e-02],\n",
       "        [  8.42480000e-02,  -1.95470000e-02],\n",
       "        [  8.45815000e-01,   9.52617000e-01],\n",
       "        [  5.76946000e-01,   1.23412900e+00],\n",
       "        [  7.72083000e-01,   1.29901800e+00],\n",
       "        [  6.96648000e-01,   8.45423000e-01],\n",
       "        [  5.95012000e-01,   1.21343500e+00],\n",
       "        [  6.48675000e-01,   1.28740700e+00],\n",
       "        [  8.97094000e-01,   1.24020900e+00],\n",
       "        [  5.52990000e-01,   1.03615800e+00],\n",
       "        [  3.32982000e-01,   2.10084000e-01],\n",
       "        [  6.56150000e-02,  -3.06970000e-01],\n",
       "        [  2.78661000e-01,   2.53628000e-01],\n",
       "        [  7.73168000e-01,   1.14091700e+00],\n",
       "        [  2.03693000e-01,  -6.40360000e-02],\n",
       "        [  3.55688000e-01,  -1.19399000e-01],\n",
       "        [  9.88852000e-01,   1.06906200e+00],\n",
       "        [  5.18735000e-01,   1.03717900e+00],\n",
       "        [  5.14563000e-01,   1.15664800e+00],\n",
       "        [  9.76414000e-01,   8.62911000e-01],\n",
       "        [  9.19074000e-01,   1.12341300e+00],\n",
       "        [  6.97777000e-01,   8.27805000e-01],\n",
       "        [  9.28097000e-01,   8.83225000e-01],\n",
       "        [  9.00272000e-01,   9.96871000e-01],\n",
       "        [  3.44102000e-01,  -6.15390000e-02],\n",
       "        [  1.48049000e-01,   2.04298000e-01],\n",
       "        [  1.30052000e-01,  -2.61670000e-02],\n",
       "        [  3.02001000e-01,   3.17135000e-01],\n",
       "        [  3.37100000e-01,   2.63320000e-02],\n",
       "        [  3.14924000e-01,  -1.95200000e-03],\n",
       "        [  2.69681000e-01,  -1.65971000e-01],\n",
       "        [  1.96005000e-01,  -4.88470000e-02],\n",
       "        [  1.29061000e-01,   3.05107000e-01],\n",
       "        [  9.36783000e-01,   1.02625800e+00],\n",
       "        [  3.05540000e-01,  -1.15991000e-01],\n",
       "        [  6.83921000e-01,   1.41438200e+00],\n",
       "        [  6.22398000e-01,   7.66330000e-01],\n",
       "        [  9.02532000e-01,   8.61601000e-01],\n",
       "        [  7.12503000e-01,   9.33490000e-01],\n",
       "        [  5.90062000e-01,   7.05531000e-01],\n",
       "        [  7.23120000e-01,   1.30724800e+00],\n",
       "        [  1.88218000e-01,   1.13685000e-01],\n",
       "        [  6.43601000e-01,   7.82552000e-01],\n",
       "        [  5.20207000e-01,   1.20955700e+00],\n",
       "        [  2.33115000e-01,  -3.48147000e-01],\n",
       "        [  4.65625000e-01,  -1.52940000e-01],\n",
       "        [  8.84512000e-01,   1.11783300e+00],\n",
       "        [  6.63200000e-01,   7.01634000e-01],\n",
       "        [  2.68857000e-01,   7.34470000e-02],\n",
       "        [  7.29234000e-01,   9.31956000e-01],\n",
       "        [  4.29664000e-01,  -1.88659000e-01],\n",
       "        [  7.37189000e-01,   1.20078100e+00],\n",
       "        [  3.78595000e-01,  -2.96094000e-01],\n",
       "        [  9.30173000e-01,   1.03564500e+00],\n",
       "        [  7.74301000e-01,   8.36763000e-01],\n",
       "        [  2.73940000e-01,  -8.57130000e-02],\n",
       "        [  8.24442000e-01,   1.08215300e+00],\n",
       "        [  6.26011000e-01,   8.40544000e-01],\n",
       "        [  6.79390000e-01,   1.30721700e+00],\n",
       "        [  5.78252000e-01,   9.21885000e-01],\n",
       "        [  7.85541000e-01,   1.16529600e+00],\n",
       "        [  5.97409000e-01,   9.74770000e-01],\n",
       "        [  1.40830000e-02,  -1.32525000e-01],\n",
       "        [  6.63870000e-01,   1.18712900e+00],\n",
       "        [  5.52381000e-01,   1.36963000e+00],\n",
       "        [  6.83886000e-01,   9.99985000e-01],\n",
       "        [  2.10334000e-01,  -6.89900000e-03],\n",
       "        [  6.04529000e-01,   1.21268500e+00],\n",
       "        [  2.50744000e-01,   4.62970000e-02]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myDat = loadDataSet('ex00.txt')\n",
    "myMat = mat(myDat)\n",
    "myMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(200, 2)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shape(myMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "def regLeaf(dataSet):#returns the value used for each leaf\n",
    "    return mean(dataSet[:,-1])\n",
    "\n",
    "def regErr(dataSet):\n",
    "    return var(dataSet[:,-1]) * shape(dataSet)[0]\n",
    "\n",
    "def linearSolve(dataSet):   #helper function used in two places\n",
    "    m,n = shape(dataSet)\n",
    "    X = mat(ones((m,n))); Y = mat(ones((m,1)))#create a copy of data with 1 in 0th postion\n",
    "    X[:,1:n] = dataSet[:,0:n-1]; Y = dataSet[:,-1]#and strip out Y\n",
    "    xTx = X.T*X\n",
    "    if linalg.det(xTx) == 0.0:\n",
    "        raise NameError('This matrix is singular, cannot do inverse,\\n\\\n",
    "        try increasing the second value of ops')\n",
    "    ws = xTx.I * (X.T * Y)\n",
    "    return ws,X,Y\n",
    "\n",
    "def modelLeaf(dataSet):#create linear model and return coeficients\n",
    "    ws,X,Y = linearSolve(dataSet)\n",
    "    return ws\n",
    "\n",
    "def modelErr(dataSet):\n",
    "    ws,X,Y = linearSolve(dataSet)\n",
    "    yHat = X * ws\n",
    "    return sum(power(Y - yHat,2))\n",
    "\n",
    "def chooseBestSplit(dataSet, leafType=regLeaf, errType=regErr, ops=(1,4)):\n",
    "    tolS = ops[0]; tolN = ops[1]\n",
    "    #if all the target variables are the same value: quit and return value\n",
    "    if len(set(dataSet[:,-1].T.tolist()[0])) == 1: #exit cond 1\n",
    "        return None, leafType(dataSet)\n",
    "    m,n = shape(dataSet)\n",
    "    #the choice of the best feature is driven by Reduction in RSS error from mean\n",
    "    S = errType(dataSet)\n",
    "    bestS = inf; bestIndex = 0; bestValue = 0\n",
    "    for featIndex in range(n-1):\n",
    "        for splitVal in set(dataSet[:,featIndex].T.tolist()[0]):\n",
    "            mat0, mat1 = binSplitDataSet(dataSet, featIndex, splitVal)\n",
    "            if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN): continue\n",
    "            newS = errType(mat0) + errType(mat1)\n",
    "            if newS < bestS: \n",
    "                bestIndex = featIndex\n",
    "                bestValue = splitVal\n",
    "                bestS = newS\n",
    "    #if the decrease (S-bestS) is less than a threshold don't do the split\n",
    "    if (S - bestS) < tolS: \n",
    "        return None, leafType(dataSet) #exit cond 2\n",
    "    mat0, mat1 = binSplitDataSet(dataSet, bestIndex, bestValue)\n",
    "    if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN):  #exit cond 3\n",
    "        return None, leafType(dataSet)\n",
    "    return bestIndex,bestValue#returns the best feature to split on\n",
    "                              #and the value used for that split\n",
    "\n",
    "def createTree(dataSet, leafType=regLeaf, errType=regErr, ops=(1,4)):#assume dataSet is NumPy Mat so we can array filtering\n",
    "    feat, val = chooseBestSplit(dataSet, leafType, errType, ops)#choose the best split\n",
    "    if feat == None: return val #if the splitting hit a stop condition return val\n",
    "    retTree = {}\n",
    "    retTree['spInd'] = feat\n",
    "    retTree['spVal'] = val\n",
    "    lSet, rSet = binSplitDataSet(dataSet, feat, val)\n",
    "    retTree['left'] = createTree(lSet, leafType, errType, ops)\n",
    "    retTree['right'] = createTree(rSet, leafType, errType, ops)\n",
    "    return retTree  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': 1.0180967672413792,\n",
       " 'right': -0.044650285714285719,\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.48813}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  1.00000000e+00,   4.09175000e-01,   1.88318000e+00],\n",
       "        [  1.00000000e+00,   1.82603000e-01,   6.39080000e-02],\n",
       "        [  1.00000000e+00,   6.63687000e-01,   3.04225700e+00],\n",
       "        [  1.00000000e+00,   5.17395000e-01,   2.30500400e+00],\n",
       "        [  1.00000000e+00,   1.36430000e-02,  -6.76980000e-02],\n",
       "        [  1.00000000e+00,   4.69643000e-01,   1.66280900e+00],\n",
       "        [  1.00000000e+00,   7.25426000e-01,   3.27574900e+00],\n",
       "        [  1.00000000e+00,   3.94350000e-01,   1.11807700e+00],\n",
       "        [  1.00000000e+00,   5.07760000e-01,   2.09505900e+00],\n",
       "        [  1.00000000e+00,   2.37395000e-01,   1.18191200e+00],\n",
       "        [  1.00000000e+00,   5.75340000e-02,   2.21663000e-01],\n",
       "        [  1.00000000e+00,   3.69820000e-01,   9.38453000e-01],\n",
       "        [  1.00000000e+00,   9.76819000e-01,   4.14940900e+00],\n",
       "        [  1.00000000e+00,   6.16051000e-01,   3.10544400e+00],\n",
       "        [  1.00000000e+00,   4.13700000e-01,   1.89627800e+00],\n",
       "        [  1.00000000e+00,   1.05279000e-01,  -1.21345000e-01],\n",
       "        [  1.00000000e+00,   6.70273000e-01,   3.16165200e+00],\n",
       "        [  1.00000000e+00,   9.52758000e-01,   4.13535800e+00],\n",
       "        [  1.00000000e+00,   2.72316000e-01,   8.59063000e-01],\n",
       "        [  1.00000000e+00,   3.03697000e-01,   1.17027200e+00],\n",
       "        [  1.00000000e+00,   4.86698000e-01,   1.68796000e+00],\n",
       "        [  1.00000000e+00,   5.11810000e-01,   1.97974500e+00],\n",
       "        [  1.00000000e+00,   1.95865000e-01,   6.86900000e-02],\n",
       "        [  1.00000000e+00,   9.86769000e-01,   4.05213700e+00],\n",
       "        [  1.00000000e+00,   7.85623000e-01,   3.15631600e+00],\n",
       "        [  1.00000000e+00,   7.97583000e-01,   2.95063000e+00],\n",
       "        [  1.00000000e+00,   8.13060000e-02,   6.89350000e-02],\n",
       "        [  1.00000000e+00,   6.59753000e-01,   2.85402000e+00],\n",
       "        [  1.00000000e+00,   3.75270000e-01,   9.99743000e-01],\n",
       "        [  1.00000000e+00,   8.19136000e-01,   4.04808200e+00],\n",
       "        [  1.00000000e+00,   1.42432000e-01,   2.30923000e-01],\n",
       "        [  1.00000000e+00,   2.15112000e-01,   8.16693000e-01],\n",
       "        [  1.00000000e+00,   4.12700000e-02,   1.30713000e-01],\n",
       "        [  1.00000000e+00,   4.41360000e-02,  -5.37706000e-01],\n",
       "        [  1.00000000e+00,   1.31337000e-01,  -3.39109000e-01],\n",
       "        [  1.00000000e+00,   4.63444000e-01,   2.12453800e+00],\n",
       "        [  1.00000000e+00,   6.71905000e-01,   2.70829200e+00],\n",
       "        [  1.00000000e+00,   9.46559000e-01,   4.01739000e+00],\n",
       "        [  1.00000000e+00,   9.04176000e-01,   4.00402100e+00],\n",
       "        [  1.00000000e+00,   3.06674000e-01,   1.02255500e+00],\n",
       "        [  1.00000000e+00,   8.19006000e-01,   3.65744200e+00],\n",
       "        [  1.00000000e+00,   8.45472000e-01,   4.07361900e+00],\n",
       "        [  1.00000000e+00,   1.56258000e-01,   1.19940000e-02],\n",
       "        [  1.00000000e+00,   8.57185000e-01,   3.64042900e+00],\n",
       "        [  1.00000000e+00,   4.00158000e-01,   1.80849700e+00],\n",
       "        [  1.00000000e+00,   3.75395000e-01,   1.43140400e+00],\n",
       "        [  1.00000000e+00,   8.85807000e-01,   3.93554400e+00],\n",
       "        [  1.00000000e+00,   2.39960000e-01,   1.16215200e+00],\n",
       "        [  1.00000000e+00,   1.48640000e-01,  -2.27330000e-01],\n",
       "        [  1.00000000e+00,   1.43143000e-01,  -6.87280000e-02],\n",
       "        [  1.00000000e+00,   3.21582000e-01,   8.25051000e-01],\n",
       "        [  1.00000000e+00,   5.09393000e-01,   2.00864500e+00],\n",
       "        [  1.00000000e+00,   3.55891000e-01,   6.64566000e-01],\n",
       "        [  1.00000000e+00,   9.38633000e-01,   4.18020200e+00],\n",
       "        [  1.00000000e+00,   3.48057000e-01,   8.64845000e-01],\n",
       "        [  1.00000000e+00,   4.38898000e-01,   1.85117400e+00],\n",
       "        [  1.00000000e+00,   7.81419000e-01,   2.76199300e+00],\n",
       "        [  1.00000000e+00,   9.11333000e-01,   4.07591400e+00],\n",
       "        [  1.00000000e+00,   3.24690000e-02,   1.10229000e-01],\n",
       "        [  1.00000000e+00,   4.99985000e-01,   2.18198700e+00],\n",
       "        [  1.00000000e+00,   7.71663000e-01,   3.15252800e+00],\n",
       "        [  1.00000000e+00,   6.70361000e-01,   3.04656400e+00],\n",
       "        [  1.00000000e+00,   1.76202000e-01,   1.28954000e-01],\n",
       "        [  1.00000000e+00,   3.92170000e-01,   1.06272600e+00],\n",
       "        [  1.00000000e+00,   9.11188000e-01,   3.65174200e+00],\n",
       "        [  1.00000000e+00,   8.72288000e-01,   4.40195000e+00],\n",
       "        [  1.00000000e+00,   7.33107000e-01,   3.02288800e+00],\n",
       "        [  1.00000000e+00,   6.10239000e-01,   2.87491700e+00],\n",
       "        [  1.00000000e+00,   7.32739000e-01,   2.94680100e+00],\n",
       "        [  1.00000000e+00,   7.14825000e-01,   2.89364400e+00],\n",
       "        [  1.00000000e+00,   7.63860000e-02,   7.21310000e-02],\n",
       "        [  1.00000000e+00,   5.59009000e-01,   1.74827500e+00],\n",
       "        [  1.00000000e+00,   4.27258000e-01,   1.91204700e+00],\n",
       "        [  1.00000000e+00,   8.41875000e-01,   3.71068600e+00],\n",
       "        [  1.00000000e+00,   5.58918000e-01,   1.71914800e+00],\n",
       "        [  1.00000000e+00,   5.33241000e-01,   2.17409000e+00],\n",
       "        [  1.00000000e+00,   9.56665000e-01,   3.65635700e+00],\n",
       "        [  1.00000000e+00,   6.20393000e-01,   3.52250400e+00],\n",
       "        [  1.00000000e+00,   5.66120000e-01,   2.23412600e+00],\n",
       "        [  1.00000000e+00,   5.23258000e-01,   1.85977200e+00],\n",
       "        [  1.00000000e+00,   4.76884000e-01,   2.09701700e+00],\n",
       "        [  1.00000000e+00,   1.76408000e-01,   1.79400000e-03],\n",
       "        [  1.00000000e+00,   3.03094000e-01,   1.23192800e+00],\n",
       "        [  1.00000000e+00,   6.09731000e-01,   2.95386200e+00],\n",
       "        [  1.00000000e+00,   1.77740000e-02,  -1.16803000e-01],\n",
       "        [  1.00000000e+00,   6.22616000e-01,   2.63886400e+00],\n",
       "        [  1.00000000e+00,   8.86539000e-01,   3.94342800e+00],\n",
       "        [  1.00000000e+00,   1.48654000e-01,  -3.28513000e-01],\n",
       "        [  1.00000000e+00,   1.04350000e-01,  -9.98660000e-02],\n",
       "        [  1.00000000e+00,   1.16868000e-01,  -3.08360000e-02],\n",
       "        [  1.00000000e+00,   5.16514000e-01,   2.35978600e+00],\n",
       "        [  1.00000000e+00,   6.64896000e-01,   3.21258100e+00],\n",
       "        [  1.00000000e+00,   4.32700000e-03,   1.88975000e-01],\n",
       "        [  1.00000000e+00,   4.25559000e-01,   1.90410900e+00],\n",
       "        [  1.00000000e+00,   7.43671000e-01,   3.00711400e+00],\n",
       "        [  1.00000000e+00,   9.35185000e-01,   3.84583400e+00],\n",
       "        [  1.00000000e+00,   6.97300000e-01,   3.07941100e+00],\n",
       "        [  1.00000000e+00,   4.44551000e-01,   1.93973900e+00],\n",
       "        [  1.00000000e+00,   6.83753000e-01,   2.88007800e+00],\n",
       "        [  1.00000000e+00,   7.55993000e-01,   3.06357700e+00],\n",
       "        [  1.00000000e+00,   9.02690000e-01,   4.11629600e+00],\n",
       "        [  1.00000000e+00,   9.44910000e-02,  -2.40963000e-01],\n",
       "        [  1.00000000e+00,   8.73831000e-01,   4.06629900e+00],\n",
       "        [  1.00000000e+00,   9.91810000e-01,   4.01183400e+00],\n",
       "        [  1.00000000e+00,   1.85611000e-01,   7.77100000e-02],\n",
       "        [  1.00000000e+00,   6.94551000e-01,   3.10306900e+00],\n",
       "        [  1.00000000e+00,   6.57275000e-01,   2.81189700e+00],\n",
       "        [  1.00000000e+00,   1.18746000e-01,  -1.04630000e-01],\n",
       "        [  1.00000000e+00,   8.43020000e-02,   2.52160000e-02],\n",
       "        [  1.00000000e+00,   9.45341000e-01,   4.33006300e+00],\n",
       "        [  1.00000000e+00,   7.85827000e-01,   3.08709100e+00],\n",
       "        [  1.00000000e+00,   5.30933000e-01,   2.26998800e+00],\n",
       "        [  1.00000000e+00,   8.79594000e-01,   4.01070100e+00],\n",
       "        [  1.00000000e+00,   6.52770000e-01,   3.11954200e+00],\n",
       "        [  1.00000000e+00,   8.79338000e-01,   3.72341100e+00],\n",
       "        [  1.00000000e+00,   7.64739000e-01,   2.79207800e+00],\n",
       "        [  1.00000000e+00,   5.04884000e-01,   2.19278700e+00],\n",
       "        [  1.00000000e+00,   5.54203000e-01,   2.08130500e+00],\n",
       "        [  1.00000000e+00,   4.93209000e-01,   1.71446300e+00],\n",
       "        [  1.00000000e+00,   3.63783000e-01,   8.85854000e-01],\n",
       "        [  1.00000000e+00,   3.16465000e-01,   1.02818700e+00],\n",
       "        [  1.00000000e+00,   5.80283000e-01,   1.95149700e+00],\n",
       "        [  1.00000000e+00,   5.42898000e-01,   1.70942700e+00],\n",
       "        [  1.00000000e+00,   1.12661000e-01,   1.44068000e-01],\n",
       "        [  1.00000000e+00,   8.16742000e-01,   3.88024000e+00],\n",
       "        [  1.00000000e+00,   2.34175000e-01,   9.21876000e-01],\n",
       "        [  1.00000000e+00,   4.02804000e-01,   1.97931600e+00],\n",
       "        [  1.00000000e+00,   7.09423000e-01,   3.08576800e+00],\n",
       "        [  1.00000000e+00,   8.67298000e-01,   3.47612200e+00],\n",
       "        [  1.00000000e+00,   9.93392000e-01,   3.99367900e+00],\n",
       "        [  1.00000000e+00,   7.11580000e-01,   3.07788000e+00],\n",
       "        [  1.00000000e+00,   1.33643000e-01,  -1.05365000e-01],\n",
       "        [  1.00000000e+00,   5.20310000e-02,  -1.64703000e-01],\n",
       "        [  1.00000000e+00,   3.66806000e-01,   1.09681400e+00],\n",
       "        [  1.00000000e+00,   6.97521000e-01,   3.09287900e+00],\n",
       "        [  1.00000000e+00,   7.87262000e-01,   2.98792600e+00],\n",
       "        [  1.00000000e+00,   4.76710000e-01,   2.06126400e+00],\n",
       "        [  1.00000000e+00,   7.21417000e-01,   2.74685400e+00],\n",
       "        [  1.00000000e+00,   2.30376000e-01,   7.16710000e-01],\n",
       "        [  1.00000000e+00,   1.04397000e-01,   1.03831000e-01],\n",
       "        [  1.00000000e+00,   1.97834000e-01,   2.37760000e-02],\n",
       "        [  1.00000000e+00,   1.29291000e-01,  -3.32990000e-02],\n",
       "        [  1.00000000e+00,   5.28528000e-01,   1.94228600e+00],\n",
       "        [  1.00000000e+00,   9.49300000e-03,  -6.33800000e-03],\n",
       "        [  1.00000000e+00,   9.98533000e-01,   3.80875300e+00],\n",
       "        [  1.00000000e+00,   3.63522000e-01,   6.52799000e-01],\n",
       "        [  1.00000000e+00,   9.01386000e-01,   4.05374700e+00],\n",
       "        [  1.00000000e+00,   8.32693000e-01,   4.56929000e+00],\n",
       "        [  1.00000000e+00,   1.19002000e-01,  -3.27730000e-02],\n",
       "        [  1.00000000e+00,   4.87638000e-01,   2.06623600e+00],\n",
       "        [  1.00000000e+00,   1.53667000e-01,   2.22785000e-01],\n",
       "        [  1.00000000e+00,   2.38619000e-01,   1.08926800e+00],\n",
       "        [  1.00000000e+00,   2.08197000e-01,   1.48778800e+00],\n",
       "        [  1.00000000e+00,   7.50921000e-01,   2.85203300e+00],\n",
       "        [  1.00000000e+00,   1.83403000e-01,   2.44860000e-02],\n",
       "        [  1.00000000e+00,   9.95608000e-01,   3.73775000e+00],\n",
       "        [  1.00000000e+00,   1.51311000e-01,   4.50170000e-02],\n",
       "        [  1.00000000e+00,   1.26804000e-01,   1.23800000e-03],\n",
       "        [  1.00000000e+00,   9.83153000e-01,   3.89276300e+00],\n",
       "        [  1.00000000e+00,   7.72495000e-01,   2.81937600e+00],\n",
       "        [  1.00000000e+00,   7.84133000e-01,   2.83066500e+00],\n",
       "        [  1.00000000e+00,   5.69340000e-02,   2.34633000e-01],\n",
       "        [  1.00000000e+00,   4.25584000e-01,   1.81078200e+00],\n",
       "        [  1.00000000e+00,   9.98709000e-01,   4.23723500e+00],\n",
       "        [  1.00000000e+00,   7.07815000e-01,   3.03476800e+00],\n",
       "        [  1.00000000e+00,   4.13816000e-01,   1.74210600e+00],\n",
       "        [  1.00000000e+00,   2.17152000e-01,   1.16925000e+00],\n",
       "        [  1.00000000e+00,   3.60503000e-01,   8.31165000e-01],\n",
       "        [  1.00000000e+00,   9.77989000e-01,   3.72937600e+00],\n",
       "        [  1.00000000e+00,   5.07953000e-01,   1.82320500e+00],\n",
       "        [  1.00000000e+00,   9.20771000e-01,   4.02197000e+00],\n",
       "        [  1.00000000e+00,   2.10542000e-01,   1.26293900e+00],\n",
       "        [  1.00000000e+00,   9.28611000e-01,   4.15951800e+00],\n",
       "        [  1.00000000e+00,   5.80373000e-01,   2.03911400e+00],\n",
       "        [  1.00000000e+00,   8.41390000e-01,   4.10183700e+00],\n",
       "        [  1.00000000e+00,   6.81530000e-01,   2.77867200e+00],\n",
       "        [  1.00000000e+00,   2.92795000e-01,   1.22828400e+00],\n",
       "        [  1.00000000e+00,   4.56918000e-01,   1.73662000e+00],\n",
       "        [  1.00000000e+00,   1.34128000e-01,  -1.95046000e-01],\n",
       "        [  1.00000000e+00,   1.62410000e-02,  -6.32150000e-02],\n",
       "        [  1.00000000e+00,   6.91214000e-01,   3.30526800e+00],\n",
       "        [  1.00000000e+00,   5.82002000e-01,   2.06362700e+00],\n",
       "        [  1.00000000e+00,   3.03102000e-01,   8.98840000e-01],\n",
       "        [  1.00000000e+00,   6.22598000e-01,   2.70169200e+00],\n",
       "        [  1.00000000e+00,   5.25024000e-01,   1.99290900e+00],\n",
       "        [  1.00000000e+00,   9.96775000e-01,   3.81139300e+00],\n",
       "        [  1.00000000e+00,   8.81025000e-01,   4.35385700e+00],\n",
       "        [  1.00000000e+00,   7.23457000e-01,   2.63564100e+00],\n",
       "        [  1.00000000e+00,   6.76346000e-01,   2.85631100e+00],\n",
       "        [  1.00000000e+00,   2.54625000e-01,   1.35268200e+00],\n",
       "        [  1.00000000e+00,   4.88632000e-01,   2.33645900e+00],\n",
       "        [  1.00000000e+00,   5.19875000e-01,   2.11165100e+00],\n",
       "        [  1.00000000e+00,   1.60176000e-01,   1.21726000e-01],\n",
       "        [  1.00000000e+00,   6.09483000e-01,   3.26460500e+00],\n",
       "        [  1.00000000e+00,   5.31881000e-01,   2.10344600e+00],\n",
       "        [  1.00000000e+00,   3.21632000e-01,   8.96855000e-01],\n",
       "        [  1.00000000e+00,   8.45148000e-01,   4.22085000e+00],\n",
       "        [  1.00000000e+00,   1.20030000e-02,  -2.17283000e-01],\n",
       "        [  1.00000000e+00,   1.88830000e-02,  -3.00577000e-01],\n",
       "        [  1.00000000e+00,   7.14760000e-02,   6.01400000e-03]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myDat1 = loadDataSet('ex0.txt')\n",
    "myMat1 = mat(myDat1)\n",
    "myMat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': 3.9871631999999999,\n",
       "   'right': 2.9836209534883724,\n",
       "   'spInd': 1,\n",
       "   'spVal': 0.797583},\n",
       "  'right': 1.980035071428571,\n",
       "  'spInd': 1,\n",
       "  'spVal': 0.582002},\n",
       " 'right': {'left': 1.0289583666666666,\n",
       "  'right': -0.023838155555555553,\n",
       "  'spInd': 1,\n",
       "  'spVal': 0.197834},\n",
       " 'spInd': 1,\n",
       " 'spVal': 0.39435}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 1.035533,\n",
       "                'right': 1.077553,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.993349},\n",
       "               'right': {'left': 0.74420699999999995,\n",
       "                'right': 1.069062,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.988852},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.989888},\n",
       "              'right': 1.227946,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.985425},\n",
       "             'right': {'left': {'left': 0.86291099999999998,\n",
       "               'right': 0.67357900000000004,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.975022},\n",
       "              'right': {'left': {'left': 1.0646899999999999,\n",
       "                'right': {'left': 0.94525499999999996,\n",
       "                 'right': 1.0229060000000001,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.950153},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.951949},\n",
       "               'right': {'left': 0.63186200000000003,\n",
       "                'right': {'left': {'left': 1.0262579999999999,\n",
       "                  'right': 1.0356449999999999,\n",
       "                  'spInd': 0,\n",
       "                  'spVal': 0.930173},\n",
       "                 'right': 0.88322500000000004,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.928097},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.936783},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.948268},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.953112},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.976414},\n",
       "            'right': {'left': {'left': {'left': {'left': 1.0298890000000001,\n",
       "                'right': 1.123413,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.919074},\n",
       "               'right': {'left': 0.86160099999999995,\n",
       "                'right': {'left': 1.0559000000000001,\n",
       "                 'right': 0.99687099999999995,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.900272},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.901056},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.902532},\n",
       "              'right': {'left': 1.2402089999999999,\n",
       "               'right': {'left': 1.077275,\n",
       "                'right': 1.1178330000000001,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.884512},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.89593},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.897094},\n",
       "             'right': {'left': {'left': {'left': 0.79700499999999996,\n",
       "                'right': 1.114825,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.869077},\n",
       "               'right': 0.71748999999999996,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.860049},\n",
       "              'right': 1.1709590000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.848921},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.877241},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.919384},\n",
       "           'right': {'left': 0.72002999999999995,\n",
       "            'right': 0.95261700000000005,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.845815},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.846455},\n",
       "          'right': {'left': {'left': 1.229373,\n",
       "            'right': {'left': 1.01058,\n",
       "             'right': {'left': 1.0821529999999999,\n",
       "              'right': 1.0866480000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.822443},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.824442},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.834078},\n",
       "           'right': {'left': 1.2808949999999999,\n",
       "            'right': 1.3259069999999999,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.820802},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.821648},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.837522},\n",
       "         'right': {'left': {'left': {'left': {'left': 0.83526400000000001,\n",
       "             'right': 1.0952059999999999,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.814825},\n",
       "            'right': {'left': 0.70660100000000003,\n",
       "             'right': {'left': 0.92403299999999999,\n",
       "              'right': 0.96572100000000005,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.795072},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.804586},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.813719},\n",
       "           'right': {'left': 0.53321399999999997,\n",
       "            'right': 0.55261400000000005,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.789625},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.79024},\n",
       "          'right': {'left': {'left': {'left': {'left': {'left': 1.1652960000000001,\n",
       "               'right': {'left': {'left': 0.88604899999999998,\n",
       "                 'right': 1.0744880000000001,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.78193},\n",
       "                'right': 0.83676300000000003,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.774301},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.782167},\n",
       "              'right': {'left': {'left': 1.1259429999999999,\n",
       "                'right': 1.140917,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.773168},\n",
       "               'right': 1.299018,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.772083},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.773422},\n",
       "             'right': {'left': {'left': {'left': {'left': 0.89970499999999998,\n",
       "                 'right': 0.76021899999999998,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.768596},\n",
       "                'right': 1.058262,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.761474},\n",
       "               'right': {'left': 0.74810399999999999,\n",
       "                'right': 0.90629099999999996,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.750078},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.750918},\n",
       "              'right': {'left': {'left': 1.087056,\n",
       "                'right': 1.2007810000000001,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.737189},\n",
       "               'right': {'left': 0.93195600000000001,\n",
       "                'right': {'left': 1.000567,\n",
       "                 'right': 1.017112,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.726828},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.727098},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.729234},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.742527},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.768784},\n",
       "            'right': 1.307248,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.72312},\n",
       "           'right': {'left': {'left': 0.93349000000000004,\n",
       "             'right': 0.56485799999999997,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.712386},\n",
       "            'right': {'left': {'left': {'left': 1.1016779999999999,\n",
       "               'right': 0.82780500000000001,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.697777},\n",
       "              'right': 1.212434,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.697718},\n",
       "             'right': {'left': 0.84542300000000004,\n",
       "              'right': 0.83439099999999999,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.691115},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.696648},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.703755},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.712503},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.785541},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.819823},\n",
       "        'right': {'left': 1.414382,\n",
       "         'right': {'left': {'left': 0.99998500000000001,\n",
       "           'right': 1.3072170000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.67939},\n",
       "          'right': {'left': {'left': 0.90772699999999995,\n",
       "            'right': 0.91507700000000003,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.673195},\n",
       "           'right': 1.1871290000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.66387},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.678287},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.683886},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.683921},\n",
       "       'right': {'left': {'left': 0.70163399999999998,\n",
       "         'right': 0.76704000000000006,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.661923},\n",
       "        'right': {'left': 0.95850599999999997,\n",
       "         'right': 1.004346,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.652121},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.656218},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.6632},\n",
       "      'right': {'left': {'left': 1.3153840000000001,\n",
       "        'right': 1.287407,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.648675},\n",
       "       'right': {'left': 1.026886,\n",
       "        'right': 1.024241,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.643665},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.645762},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.651376},\n",
       "     'right': {'left': 0.78255200000000003,\n",
       "      'right': 0.84054399999999996,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.626011},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.643601},\n",
       "    'right': 1.244731,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.625791},\n",
       "   'right': {'left': 0.62369600000000003,\n",
       "    'right': 0.76632999999999996,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.622398},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.625336},\n",
       "  'right': {'left': {'left': 1.3344210000000001,\n",
       "    'right': 1.6210910000000001,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.613765},\n",
       "   'right': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 0.98203600000000002,\n",
       "          'right': 1.212685,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.604529},\n",
       "         'right': 0.97477000000000003,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.597409},\n",
       "        'right': {'left': 1.213435,\n",
       "         'right': 1.3366610000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.59021},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.595012},\n",
       "       'right': {'left': 0.70553100000000002,\n",
       "        'right': {'left': {'left': {'left': 1.1858120000000001,\n",
       "           'right': 0.92188499999999995,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.578252},\n",
       "          'right': 1.234129,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.576946},\n",
       "         'right': {'left': 0.89908999999999994,\n",
       "          'right': {'left': {'left': 1.06613,\n",
       "            'right': 0.96905799999999997,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.567704},\n",
       "           'right': 1.0705290000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.561362},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.574573},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.575805},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.589575},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.590062},\n",
       "      'right': {'left': {'left': {'left': 1.2531509999999999,\n",
       "         'right': 1.391273,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.55352},\n",
       "        'right': 1.0361579999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.55299},\n",
       "       'right': 1.3696299999999999,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.552381},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.559763},\n",
       "     'right': {'left': {'left': {'left': 0.89374799999999999,\n",
       "        'right': 1.0538460000000001,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.539558},\n",
       "       'right': {'left': {'left': 0.86728400000000005,\n",
       "         'right': 0.89346199999999998,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.530897},\n",
       "        'right': {'left': 1.0222059999999999,\n",
       "         'right': 0.87956000000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.527505},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.529491},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.536689},\n",
       "      'right': {'left': 1.209557,\n",
       "       'right': {'left': 0.96198300000000003,\n",
       "        'right': 1.0371790000000001,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.518735},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.520044},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.520207},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.541314},\n",
       "    'right': {'left': 1.4935860000000001,\n",
       "     'right': {'left': 1.1566479999999999,\n",
       "      'right': 1.1072649999999999,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.50794},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.514563},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.517921},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.61127},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.620599},\n",
       " 'right': {'left': {'left': {'left': {'left': {'left': -0.097791000000000003,\n",
       "      'right': {'left': -0.16370699999999999,\n",
       "       'right': -0.15293999999999999,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.465625},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.475976},\n",
       "     'right': {'left': {'left': -0.061455999999999997,\n",
       "       'right': {'left': 0.069098000000000007,\n",
       "        'right': {'left': 0.026974000000000001,\n",
       "         'right': 0.034014000000000003,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.438367},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.448656},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.44928},\n",
       "      'right': -0.18865899999999999,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.429664},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.458121},\n",
       "    'right': 0.33172200000000002,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.41023},\n",
       "   'right': {'left': {'left': {'left': {'left': -0.366317,\n",
       "       'right': {'left': -0.12164,\n",
       "        'right': -0.29609400000000002,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.378595},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.401152},\n",
       "      'right': {'left': 0.088505,\n",
       "       'right': -0.24354999999999999,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.377201},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.377597},\n",
       "     'right': {'left': -0.55646399999999996,\n",
       "      'right': -0.20483000000000001,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.360323},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.362314},\n",
       "    'right': {'left': {'left': {'left': -0.11939900000000001,\n",
       "       'right': 0.048939000000000003,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.348013},\n",
       "      'right': {'left': {'left': -0.15340500000000001,\n",
       "        'right': -0.15038899999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.346986},\n",
       "       'right': -0.061539000000000003,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.344102},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.347837},\n",
       "     'right': -0.37169999999999997,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.343554},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.355688},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.406649},\n",
       "  'right': {'left': {'left': {'left': 0.175264,\n",
       "     'right': 0.20678299999999999,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.339563},\n",
       "    'right': {'left': 0.026332000000000001,\n",
       "     'right': 0.21008399999999999,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.332982},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.3371},\n",
       "   'right': {'left': {'left': {'left': {'left': {'left': {'left': -0.219245,\n",
       "         'right': {'left': 0.180811,\n",
       "          'right': {'left': {'left': {'left': {'left': {'left': -0.001952,\n",
       "               'right': {'left': -0.17732100000000001,\n",
       "                'right': {'left': -0.115991,\n",
       "                 'right': -0.14865,\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.302217},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.30554},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.306964},\n",
       "              'right': {'left': 0.317135,\n",
       "               'right': 0.002882,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.295511},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.302001},\n",
       "             'right': -0.22888,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.280738},\n",
       "            'right': 0.25362800000000002,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.278661},\n",
       "           'right': {'left': {'left': -0.085712999999999998,\n",
       "             'right': {'left': -0.45521899999999998,\n",
       "              'right': -0.16597100000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.269681},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.273147},\n",
       "            'right': {'left': 0.073446999999999998,\n",
       "             'right': {'left': -0.055613000000000003,\n",
       "              'right': {'left': 0.046296999999999998,\n",
       "               'right': -0.029467,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.243909},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.250744},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.252649},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.268857},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.27394},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.314924},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.323181},\n",
       "        'right': 0.20935899999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.242204},\n",
       "       'right': {'left': {'left': -0.35845900000000003,\n",
       "         'right': -0.34814699999999998,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.233115},\n",
       "        'right': {'left': {'left': -0.0068989999999999998,\n",
       "          'right': {'left': {'left': -0.064035999999999996,\n",
       "            'right': -0.087744000000000003,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.202054},\n",
       "           'right': -0.048847000000000002,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.196005},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.203693},\n",
       "         'right': -0.32758900000000002,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.193641},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.210334},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.23807},\n",
       "      'right': {'left': {'left': {'left': {'left': {'left': 0.11368499999999999,\n",
       "           'right': 0.18484300000000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.18351},\n",
       "          'right': {'left': 0.103676,\n",
       "           'right': 0.132858,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.152324},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.180506},\n",
       "         'right': 0.20429800000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.148049},\n",
       "        'right': {'left': 0.034283000000000001,\n",
       "         'right': 0.13697899999999999,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.145809},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.146366},\n",
       "       'right': {'left': {'left': -0.22314300000000001,\n",
       "         'right': -0.329372,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.132543},\n",
       "        'right': {'left': {'left': {'left': 0.18424099999999999,\n",
       "           'right': -0.026166999999999999,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.130052},\n",
       "          'right': 0.30510700000000002,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.129061},\n",
       "         'right': {'left': {'left': {'left': -0.077409000000000006,\n",
       "            'right': {'left': 0.068834000000000006,\n",
       "             'right': 0.02528,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.099142},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.101149},\n",
       "           'right': -0.33276,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.098016},\n",
       "          'right': {'left': 0.22716700000000001,\n",
       "           'right': {'left': 0.099934999999999996,\n",
       "            'right': -0.019546999999999998,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.084248},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.091358},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.096994},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.118156},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.130962},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.1333},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.188218},\n",
       "     'right': {'left': {'left': -0.269756,\n",
       "       'right': {'left': -0.349692,\n",
       "        'right': -0.420983,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.072243},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.074795},\n",
       "      'right': {'left': {'left': -0.110946,\n",
       "        'right': 0.052439,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.066172},\n",
       "       'right': -0.30697000000000002,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.065615},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.071769},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.081931},\n",
       "    'right': {'left': {'left': 0.064495999999999998,\n",
       "      'right': {'left': 0.40815499999999999,\n",
       "       'right': 0.15509600000000001,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.036098},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.036492},\n",
       "     'right': {'left': -0.132525,\n",
       "      'right': {'left': 0.056593999999999998,\n",
       "       'right': {'left': {'left': 0.069975999999999997,\n",
       "         'right': 0.094149999999999998,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.007044},\n",
       "        'right': 0.060902999999999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.000234},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.008307},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.009849},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.014083},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.048014},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.325412},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.343479},\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.48813}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat, ops=(0,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  2.28628000e-01,  -2.26627300e+00],\n",
       "        [  9.65969000e-01,   1.12386764e+02],\n",
       "        [  3.42761000e-01,  -3.15848550e+01],\n",
       "        [  9.01444000e-01,   8.73006250e+01],\n",
       "        [  5.85413000e-01,   1.25295113e+02],\n",
       "        [  3.34900000e-01,   1.89766500e+01],\n",
       "        [  7.69043000e-01,   6.40419410e+01],\n",
       "        [  2.97107000e-01,  -1.79837700e+00],\n",
       "        [  9.01421000e-01,   1.00133819e+02],\n",
       "        [  1.76523000e-01,   9.46348000e-01],\n",
       "        [  7.10234000e-01,   1.08553919e+02],\n",
       "        [  9.81980000e-01,   8.63996370e+01],\n",
       "        [  8.58730000e-02,  -1.01371040e+01],\n",
       "        [  5.37834000e-01,   9.09955360e+01],\n",
       "        [  8.06158000e-01,   6.28776980e+01],\n",
       "        [  7.08890000e-01,   1.35416767e+02],\n",
       "        [  7.87755000e-01,   1.18642009e+02],\n",
       "        [  4.63241000e-01,   1.71710570e+01],\n",
       "        [  3.00318000e-01,  -1.80513180e+01],\n",
       "        [  8.15215000e-01,   1.18319942e+02],\n",
       "        [  1.39880000e-01,   7.33678400e+00],\n",
       "        [  6.83730000e-02,  -1.51608360e+01],\n",
       "        [  4.57563000e-01,  -3.40445550e+01],\n",
       "        [  6.65652000e-01,   1.05547997e+02],\n",
       "        [  8.46610000e-02,  -2.41322260e+01],\n",
       "        [  9.54711000e-01,   1.00935789e+02],\n",
       "        [  9.53902000e-01,   1.30926480e+02],\n",
       "        [  4.87381000e-01,   2.77292630e+01],\n",
       "        [  7.59504000e-01,   8.11067620e+01],\n",
       "        [  4.54312000e-01,  -2.03600670e+01],\n",
       "        [  2.95993000e-01,  -1.49882790e+01],\n",
       "        [  1.56067000e-01,   7.55734900e+00],\n",
       "        [  4.28582000e-01,   1.52242660e+01],\n",
       "        [  8.47219000e-01,   7.62409840e+01],\n",
       "        [  4.99171000e-01,   1.19242040e+01],\n",
       "        [  2.03993000e-01,  -2.23791190e+01],\n",
       "        [  5.48539000e-01,   8.31145020e+01],\n",
       "        [  7.90312000e-01,   1.10159730e+02],\n",
       "        [  9.37766000e-01,   1.19949824e+02],\n",
       "        [  2.18321000e-01,   1.41076800e+00],\n",
       "        [  2.23200000e-01,   1.55016420e+01],\n",
       "        [  8.96683000e-01,   1.07001620e+02],\n",
       "        [  5.82311000e-01,   8.25893280e+01],\n",
       "        [  6.98920000e-01,   9.24706360e+01],\n",
       "        [  8.23848000e-01,   5.93423230e+01],\n",
       "        [  3.85021000e-01,   2.48169410e+01],\n",
       "        [  6.12190000e-02,   6.69556700e+00],\n",
       "        [  8.41547000e-01,   1.15669032e+02],\n",
       "        [  7.63328000e-01,   1.15199195e+02],\n",
       "        [  9.34853000e-01,   1.15753994e+02],\n",
       "        [  2.22271000e-01,  -9.25585200e+00],\n",
       "        [  2.17214000e-01,  -3.95875200e+00],\n",
       "        [  7.06961000e-01,   1.06180427e+02],\n",
       "        [  8.88426000e-01,   9.48963540e+01],\n",
       "        [  5.49814000e-01,   1.37267576e+02],\n",
       "        [  1.07960000e-01,  -1.29319500e+00],\n",
       "        [  8.51110000e-02,   3.78206590e+01],\n",
       "        [  3.88789000e-01,   2.15780070e+01],\n",
       "        [  4.67383000e-01,  -9.71292500e+00],\n",
       "        [  6.23909000e-01,   8.71818630e+01],\n",
       "        [  3.73501000e-01,  -8.22829700e+00],\n",
       "        [  5.13332000e-01,   1.01075609e+02],\n",
       "        [  3.50725000e-01,  -4.00865640e+01],\n",
       "        [  7.16211000e-01,   1.03345308e+02],\n",
       "        [  7.31636000e-01,   7.39120280e+01],\n",
       "        [  2.73863000e-01,  -9.45755600e+00],\n",
       "        [  2.11633000e-01,  -8.33220700e+00],\n",
       "        [  9.44221000e-01,   1.00120253e+02],\n",
       "        [  5.37640000e-02,  -1.37316980e+01],\n",
       "        [  1.26833000e-01,   2.28916750e+01],\n",
       "        [  9.52833000e-01,   1.00649591e+02],\n",
       "        [  3.91609000e-01,   3.00110400e+00],\n",
       "        [  5.60301000e-01,   8.29039450e+01],\n",
       "        [  1.24723000e-01,  -1.40279600e+00],\n",
       "        [  4.65680000e-01,  -2.37775310e+01],\n",
       "        [  6.99873000e-01,   1.15586605e+02],\n",
       "        [  1.64134000e-01,  -2.74052110e+01],\n",
       "        [  4.55761000e-01,   9.84193800e+00],\n",
       "        [  5.08542000e-01,   9.64033730e+01],\n",
       "        [  1.38619000e-01,  -2.90874630e+01],\n",
       "        [  3.35182000e-01,   2.76822500e+00],\n",
       "        [  9.08629000e-01,   1.18513475e+02],\n",
       "        [  5.46601000e-01,   9.63190430e+01],\n",
       "        [  3.78965000e-01,   1.35835550e+01],\n",
       "        [  9.68621000e-01,   9.86483460e+01],\n",
       "        [  6.37999000e-01,   9.16566170e+01],\n",
       "        [  3.50065000e-01,  -1.31985200e+00],\n",
       "        [  6.32691000e-01,   9.36452930e+01],\n",
       "        [  9.36524000e-01,   6.55484180e+01],\n",
       "        [  3.10956000e-01,  -4.99395160e+01],\n",
       "        [  4.37652000e-01,   1.97452240e+01],\n",
       "        [  1.66765000e-01,  -1.47400590e+01],\n",
       "        [  5.71214000e-01,   1.14872056e+02],\n",
       "        [  9.52377000e-01,   7.35208020e+01],\n",
       "        [  6.65329000e-01,   1.21980607e+02],\n",
       "        [  2.58070000e-01,  -2.04251370e+01],\n",
       "        [  9.12161000e-01,   8.50053510e+01],\n",
       "        [  7.77582000e-01,   1.00838446e+02],\n",
       "        [  6.42707000e-01,   8.25007660e+01],\n",
       "        [  8.85676000e-01,   1.08045948e+02],\n",
       "        [  8.00610000e-02,   2.22987300e+00],\n",
       "        [  3.99140000e-02,   1.12200990e+01],\n",
       "        [  9.58512000e-01,   1.35837013e+02],\n",
       "        [  3.77383000e-01,   5.24119600e+00],\n",
       "        [  6.61073000e-01,   1.15687524e+02],\n",
       "        [  4.54375000e-01,   3.04391200e+00],\n",
       "        [  4.12516000e-01,  -2.64192890e+01],\n",
       "        [  8.54970000e-01,   8.92099300e+01],\n",
       "        [  6.98472000e-01,   1.20521925e+02],\n",
       "        [  4.65561000e-01,   3.00519310e+01],\n",
       "        [  3.28890000e-01,   3.97831130e+01],\n",
       "        [  3.09133000e-01,   8.81472500e+00],\n",
       "        [  4.18943000e-01,   4.41614930e+01],\n",
       "        [  5.53797000e-01,   1.20857321e+02],\n",
       "        [  7.99873000e-01,   9.13684730e+01],\n",
       "        [  8.11363000e-01,   1.12981216e+02],\n",
       "        [  7.85574000e-01,   1.07024467e+02],\n",
       "        [  9.49198000e-01,   1.05752508e+02],\n",
       "        [  6.66452000e-01,   1.20014736e+02],\n",
       "        [  6.52462000e-01,   1.12715799e+02],\n",
       "        [  2.90749000e-01,  -1.43916130e+01],\n",
       "        [  5.08548000e-01,   9.32928290e+01],\n",
       "        [  6.80486000e-01,   1.10367074e+02],\n",
       "        [  3.56790000e-01,  -1.95265390e+01],\n",
       "        [  1.99903000e-01,  -3.37247200e+00],\n",
       "        [  2.64926000e-01,   5.28057900e+00],\n",
       "        [  1.66431000e-01,  -6.51250600e+00],\n",
       "        [  3.70042000e-01,  -3.21244950e+01],\n",
       "        [  6.28061000e-01,   1.17628346e+02],\n",
       "        [  2.28473000e-01,   1.94251580e+01],\n",
       "        [  4.47370000e-02,   3.85539300e+00],\n",
       "        [  1.93282000e-01,   1.82084230e+01],\n",
       "        [  5.19150000e-01,   1.16176162e+02],\n",
       "        [  3.51478000e-01,  -4.61116000e-01],\n",
       "        [  8.72199000e-01,   1.11552716e+02],\n",
       "        [  1.15150000e-01,   1.37958280e+01],\n",
       "        [  3.24274000e-01,  -1.31892430e+01],\n",
       "        [  4.46196000e-01,  -5.10817200e+00],\n",
       "        [  6.13004000e-01,   1.68180746e+02],\n",
       "        [  5.33511000e-01,   1.29766743e+02],\n",
       "        [  7.40859000e-01,   9.37739290e+01],\n",
       "        [  6.67851000e-01,   9.24496640e+01],\n",
       "        [  9.00699000e-01,   1.09188248e+02],\n",
       "        [  5.99142000e-01,   1.30378529e+02],\n",
       "        [  2.32802000e-01,   1.22231800e+00],\n",
       "        [  8.38587000e-01,   1.34089674e+02],\n",
       "        [  2.84794000e-01,   3.56237460e+01],\n",
       "        [  1.30626000e-01,  -3.95244610e+01],\n",
       "        [  6.42373000e-01,   1.40613941e+02],\n",
       "        [  7.86865000e-01,   1.00598825e+02],\n",
       "        [  4.03228000e-01,  -1.72924400e+00],\n",
       "        [  8.83615000e-01,   9.53481840e+01],\n",
       "        [  9.10975000e-01,   1.06814667e+02],\n",
       "        [  8.19722000e-01,   7.00545080e+01],\n",
       "        [  7.98198000e-01,   7.68537280e+01],\n",
       "        [  6.06417000e-01,   9.35213960e+01],\n",
       "        [  1.08801000e-01,  -1.61061640e+01],\n",
       "        [  3.18309000e-01,  -2.76054240e+01],\n",
       "        [  8.56421000e-01,   1.07166848e+02],\n",
       "        [  8.42940000e-01,   9.58931310e+01],\n",
       "        [  6.18868000e-01,   7.69176650e+01],\n",
       "        [  5.31944000e-01,   1.24795495e+02],\n",
       "        [  2.85460000e-02,  -8.37709400e+00],\n",
       "        [  9.15263000e-01,   9.67176100e+01],\n",
       "        [  9.25782000e-01,   9.20746190e+01],\n",
       "        [  6.24827000e-01,   1.05970743e+02],\n",
       "        [  3.31364000e-01,  -1.29082500e+00],\n",
       "        [  3.41700000e-01,  -2.35477110e+01],\n",
       "        [  3.42155000e-01,  -1.69304160e+01],\n",
       "        [  7.29397000e-01,   1.10902830e+02],\n",
       "        [  6.40515000e-01,   8.27136210e+01],\n",
       "        [  2.28751000e-01,  -3.08129120e+01],\n",
       "        [  9.48822000e-01,   6.93186490e+01],\n",
       "        [  7.06390000e-01,   1.05062147e+02],\n",
       "        [  7.96320000e-02,   2.94200680e+01],\n",
       "        [  4.51087000e-01,  -2.87246850e+01],\n",
       "        [  8.33026000e-01,   7.67238350e+01],\n",
       "        [  5.89806000e-01,   9.86748740e+01],\n",
       "        [  4.26711000e-01,  -2.15942680e+01],\n",
       "        [  8.72883000e-01,   9.58877120e+01],\n",
       "        [  8.66451000e-01,   9.44021020e+01],\n",
       "        [  9.60398000e-01,   1.23559747e+02],\n",
       "        [  4.83803000e-01,   5.22423400e+00],\n",
       "        [  8.11602000e-01,   9.98413790e+01],\n",
       "        [  7.57527000e-01,   6.35498540e+01],\n",
       "        [  5.69327000e-01,   1.08435392e+02],\n",
       "        [  8.41625000e-01,   6.05523080e+01],\n",
       "        [  2.64639000e-01,   2.55792300e+00],\n",
       "        [  2.02161000e-01,  -1.98388900e+00],\n",
       "        [  5.58620000e-02,  -3.13149700e+00],\n",
       "        [  5.43843000e-01,   9.83620100e+01],\n",
       "        [  6.89099000e-01,   1.12378209e+02],\n",
       "        [  9.56951000e-01,   8.20165410e+01],\n",
       "        [  3.82037000e-01,  -2.90077830e+01],\n",
       "        [  1.31833000e-01,   2.24782910e+01],\n",
       "        [  1.56273000e-01,   2.25886000e-01],\n",
       "        [  2.56000000e-04,   9.66810600e+00],\n",
       "        [  8.92999000e-01,   8.24366860e+01],\n",
       "        [  2.06207000e-01,  -1.26190360e+01],\n",
       "        [  4.87537000e-01,   5.14933600e+00]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myDat2 = loadDataSet('ex2.txt')\n",
    "myMat2 = mat(myDat2)\n",
    "myMat2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': {'left': 105.24862350000001,\n",
       "    'right': 112.42895575000001,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.958512},\n",
       "   'right': {'left': {'left': {'left': {'left': 87.310387500000004,\n",
       "       'right': {'left': {'left': 96.452866999999998,\n",
       "         'right': {'left': 104.82540899999999,\n",
       "          'right': {'left': 95.181792999999999,\n",
       "           'right': 102.25234449999999,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.872883},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.892999},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.910975},\n",
       "        'right': 95.275843166666661,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.85497},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.944221},\n",
       "      'right': {'left': 81.110151999999999,\n",
       "       'right': 88.784498800000009,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.811602},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.833026},\n",
       "     'right': 102.35780185714285,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.790312},\n",
       "    'right': 78.085643250000004,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.759504},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.952833},\n",
       "  'right': {'left': {'left': {'left': 114.554706,\n",
       "     'right': {'left': 104.82495374999999,\n",
       "      'right': 108.92921799999999,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.698472},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.706961},\n",
       "    'right': 114.15162428571431,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.666452},\n",
       "   'right': {'left': 93.673449714285724,\n",
       "    'right': {'left': 123.2101316,\n",
       "     'right': {'left': 97.200180249999988,\n",
       "      'right': {'left': {'left': 109.38961049999999,\n",
       "        'right': 110.979946,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.543843},\n",
       "       'right': 101.73699325000001,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.51915},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.553797},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.582311},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.613004},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.640515},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.729397},\n",
       " 'right': {'left': {'left': 12.50675925,\n",
       "   'right': 3.4331330000000007,\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.467383},\n",
       "  'right': {'left': {'left': {'left': -12.558604833333334,\n",
       "     'right': {'left': 14.38417875,\n",
       "      'right': {'left': -0.89235549999999952,\n",
       "       'right': 3.6584772500000016,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.385021},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.412516},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.437652},\n",
       "    'right': {'left': {'left': -15.085111749999999,\n",
       "      'right': -22.693879600000002,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.350725},\n",
       "     'right': {'left': 15.059290750000001,\n",
       "      'right': {'left': -19.994155200000002,\n",
       "       'right': {'left': {'left': {'left': {'left': {'left': 0.40377471428571476,\n",
       "            'right': -13.070501,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.25807},\n",
       "           'right': 6.770429,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.228473},\n",
       "          'right': -11.822278500000001,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.217214},\n",
       "         'right': 3.4496025000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.202161},\n",
       "        'right': {'left': -12.107972500000001,\n",
       "         'right': -6.2479000000000013,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.156067},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.166765},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.297107},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.324274},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.335182},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.373501},\n",
       "   'right': {'left': 6.5098432857142843,\n",
       "    'right': {'left': -2.5443927142857148,\n",
       "     'right': 4.0916259999999998,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.044737},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.084661},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.126833},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.457563},\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.499171}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': 101.35815937735848,\n",
       " 'right': -2.6377193297872341,\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.499171}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(myMat2, ops=(10000, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isTree(obj):\n",
    "    return (type(obj).__name__=='dict')\n",
    "\n",
    "def getMean(tree):\n",
    "    if isTree(tree['right']): tree['right'] = getMean(tree['right'])\n",
    "    if isTree(tree['left']): tree['left'] = getMean(tree['left'])\n",
    "    return (tree['left']+tree['right'])/2.0\n",
    "    \n",
    "def prune(tree, testData):\n",
    "    if shape(testData)[0] == 0: return getMean(tree) #if we have no test data collapse the tree\n",
    "    if (isTree(tree['right']) or isTree(tree['left'])):#if the branches are not trees try to prune them\n",
    "        lSet, rSet = binSplitDataSet(testData, tree['spInd'], tree['spVal'])\n",
    "    if isTree(tree['left']): tree['left'] = prune(tree['left'], lSet)\n",
    "    if isTree(tree['right']): tree['right'] =  prune(tree['right'], rSet)\n",
    "    #if they are now both leafs, see if we can merge them\n",
    "    if not isTree(tree['left']) and not isTree(tree['right']):\n",
    "        lSet, rSet = binSplitDataSet(testData, tree['spInd'], tree['spVal'])\n",
    "        errorNoMerge = sum(power(lSet[:,-1] - tree['left'],2)) +\\\n",
    "            sum(power(rSet[:,-1] - tree['right'],2))\n",
    "        treeMean = (tree['left']+tree['right'])/2.0\n",
    "        errorMerge = sum(power(testData[:,-1] - treeMean,2))\n",
    "        if errorMerge < errorNoMerge: \n",
    "            print(\"merging\")\n",
    "            return treeMean\n",
    "        else: return tree\n",
    "    else: return tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "myTree = createTree(myMat2, ops=(0,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  4.21862000e-01,   1.08302410e+01],\n",
       "        [  1.05349000e-01,  -2.24161100e+00],\n",
       "        [  1.55196000e-01,   2.18729760e+01],\n",
       "        [  1.61152000e-01,   2.01541800e+00],\n",
       "        [  3.82632000e-01,  -3.87789790e+01],\n",
       "        [  1.77100000e-02,   2.01091130e+01],\n",
       "        [  1.29656000e-01,   1.52668870e+01],\n",
       "        [  6.13926000e-01,   1.11900063e+02],\n",
       "        [  4.09277000e-01,   1.87473100e+00],\n",
       "        [  8.07556000e-01,   1.11223754e+02],\n",
       "        [  5.93722000e-01,   1.33835486e+02],\n",
       "        [  9.53239000e-01,   1.10465070e+02],\n",
       "        [  2.57402000e-01,   1.53328990e+01],\n",
       "        [  6.45385000e-01,   9.39830540e+01],\n",
       "        [  5.63460000e-01,   9.36452770e+01],\n",
       "        [  4.08338000e-01,  -3.07198780e+01],\n",
       "        [  8.74394000e-01,   9.18735050e+01],\n",
       "        [  2.63805000e-01,  -1.92752000e-01],\n",
       "        [  4.11198000e-01,   1.07511180e+01],\n",
       "        [  4.49884000e-01,   9.21190100e+00],\n",
       "        [  6.46315000e-01,   1.13533660e+02],\n",
       "        [  6.73718000e-01,   1.25135638e+02],\n",
       "        [  8.05148000e-01,   1.13300462e+02],\n",
       "        [  7.59327000e-01,   7.26685720e+01],\n",
       "        [  5.19172000e-01,   8.21316980e+01],\n",
       "        [  7.41031000e-01,   1.06777146e+02],\n",
       "        [  3.09370000e-02,   9.85912700e+00],\n",
       "        [  2.68848000e-01,  -3.41379550e+01],\n",
       "        [  4.74901000e-01,  -1.12013010e+01],\n",
       "        [  5.88266000e-01,   1.20501998e+02],\n",
       "        [  8.93936000e-01,   1.42826476e+02],\n",
       "        [  8.70990000e-01,   1.05751746e+02],\n",
       "        [  4.30763000e-01,   3.91462580e+01],\n",
       "        [  5.76650000e-02,   1.53718970e+01],\n",
       "        [  1.00076000e-01,   9.13176100e+00],\n",
       "        [  9.80716000e-01,   1.16145896e+02],\n",
       "        [  2.35289000e-01,  -1.36912240e+01],\n",
       "        [  2.28098000e-01,   1.60891510e+01],\n",
       "        [  6.22248000e-01,   9.93455510e+01],\n",
       "        [  4.01467000e-01,  -1.69438300e+00],\n",
       "        [  9.60334000e-01,   1.10795415e+02],\n",
       "        [  3.12140000e-02,  -5.33004200e+00],\n",
       "        [  5.04228000e-01,   9.60035250e+01],\n",
       "        [  7.79660000e-01,   7.59215820e+01],\n",
       "        [  5.04496000e-01,   1.01341462e+02],\n",
       "        [  8.50974000e-01,   9.62930640e+01],\n",
       "        [  7.01119000e-01,   1.02333839e+02],\n",
       "        [  1.91551000e-01,   5.07232600e+00],\n",
       "        [  6.67116000e-01,   9.23100190e+01],\n",
       "        [  5.55584000e-01,   8.03671290e+01],\n",
       "        [  6.80006000e-01,   1.32965442e+02],\n",
       "        [  3.93899000e-01,   3.86052830e+01],\n",
       "        [  4.89400000e-02,  -9.86187100e+00],\n",
       "        [  9.63282000e-01,   1.15407485e+02],\n",
       "        [  6.55496000e-01,   1.04269918e+02],\n",
       "        [  5.76463000e-01,   1.41127267e+02],\n",
       "        [  6.75708000e-01,   9.62279960e+01],\n",
       "        [  8.53457000e-01,   1.14252288e+02],\n",
       "        [  3.93300000e-03,  -1.21828610e+01],\n",
       "        [  5.49512000e-01,   9.79272240e+01],\n",
       "        [  2.18967000e-01,  -4.71246200e+00],\n",
       "        [  6.59972000e-01,   1.20950439e+02],\n",
       "        [  8.25600000e-03,   8.02681600e+00],\n",
       "        [  9.95000000e-02,  -1.43184340e+01],\n",
       "        [  3.52215000e-01,  -3.74754600e+00],\n",
       "        [  8.74926000e-01,   8.92473560e+01],\n",
       "        [  6.35084000e-01,   9.94960590e+01],\n",
       "        [  3.96410000e-02,   1.41471090e+01],\n",
       "        [  6.65111000e-01,   1.03298719e+02],\n",
       "        [  1.56583000e-01,  -2.54070300e+00],\n",
       "        [  6.48843000e-01,   1.19333019e+02],\n",
       "        [  8.93237000e-01,   9.52095850e+01],\n",
       "        [  1.28807000e-01,   5.55847900e+00],\n",
       "        [  1.37438000e-01,   5.56768500e+00],\n",
       "        [  6.30538000e-01,   9.84627920e+01],\n",
       "        [  2.96084000e-01,  -4.17994380e+01],\n",
       "        [  6.32099000e-01,   8.48950980e+01],\n",
       "        [  9.87681000e-01,   1.06726447e+02],\n",
       "        [  7.44909000e-01,   1.11279705e+02],\n",
       "        [  8.62030000e-01,   1.04581156e+02],\n",
       "        [  8.06490000e-02,  -7.67998500e+00],\n",
       "        [  8.31277000e-01,   5.90533560e+01],\n",
       "        [  1.98716000e-01,   2.68788010e+01],\n",
       "        [  8.60932000e-01,   9.06329300e+01],\n",
       "        [  8.83250000e-01,   9.27595950e+01],\n",
       "        [  8.18003000e-01,   1.10272219e+02],\n",
       "        [  9.49216000e-01,   1.15200237e+02],\n",
       "        [  4.60078000e-01,  -3.59579810e+01],\n",
       "        [  5.61077000e-01,   9.35457610e+01],\n",
       "        [  8.63767000e-01,   1.14125786e+02],\n",
       "        [  4.76891000e-01,  -2.97740600e+01],\n",
       "        [  5.37826000e-01,   8.15879220e+01],\n",
       "        [  6.86224000e-01,   1.10911198e+02],\n",
       "        [  9.82327000e-01,   1.19114523e+02],\n",
       "        [  9.44453000e-01,   9.20334810e+01],\n",
       "        [  7.82270000e-02,   3.02168730e+01],\n",
       "        [  7.82937000e-01,   9.25886460e+01],\n",
       "        [  4.65886000e-01,   2.22213900e+00],\n",
       "        [  8.85024000e-01,   9.02478900e+01],\n",
       "        [  1.86077000e-01,   7.14441500e+00],\n",
       "        [  9.15828000e-01,   8.40100740e+01],\n",
       "        [  7.96649000e-01,   1.15572156e+02],\n",
       "        [  1.27821000e-01,   2.89336880e+01],\n",
       "        [  4.33429000e-01,   6.78257500e+00],\n",
       "        [  9.46796000e-01,   1.08574116e+02],\n",
       "        [  3.86915000e-01,  -1.74046010e+01],\n",
       "        [  5.61192000e-01,   9.21427000e+01],\n",
       "        [  1.82490000e-01,   1.07646160e+01],\n",
       "        [  8.78792000e-01,   9.52894760e+01],\n",
       "        [  3.81342000e-01,  -6.17746400e+00],\n",
       "        [  3.58474000e-01,  -1.17317540e+01],\n",
       "        [  2.70647000e-01,   1.37932010e+01],\n",
       "        [  4.88904000e-01,  -1.76418320e+01],\n",
       "        [  1.06773000e-01,   5.68475700e+00],\n",
       "        [  2.70112000e-01,   4.33567500e+00],\n",
       "        [  7.54985000e-01,   7.58604330e+01],\n",
       "        [  5.85174000e-01,   1.11640154e+02],\n",
       "        [  4.58821000e-01,   1.20296920e+01],\n",
       "        [  2.18017000e-01,  -2.62348720e+01],\n",
       "        [  5.83887000e-01,   9.94138500e+01],\n",
       "        [  9.23626000e-01,   1.07802298e+02],\n",
       "        [  8.33620000e-01,   1.04179678e+02],\n",
       "        [  8.70691000e-01,   9.31325910e+01],\n",
       "        [  2.49896000e-01,  -8.61840400e+00],\n",
       "        [  7.48230000e-01,   1.09160652e+02],\n",
       "        [  1.93650000e-02,   3.40488840e+01],\n",
       "        [  8.37588000e-01,   1.01239275e+02],\n",
       "        [  5.29251000e-01,   1.15514729e+02],\n",
       "        [  7.42898000e-01,   6.70387710e+01],\n",
       "        [  5.22034000e-01,   6.41607990e+01],\n",
       "        [  4.98982000e-01,   3.98306100e+00],\n",
       "        [  4.79439000e-01,   2.43559080e+01],\n",
       "        [  3.14834000e-01,  -1.42562000e+01],\n",
       "        [  7.53251000e-01,   8.50170920e+01],\n",
       "        [  4.79362000e-01,  -1.74804460e+01],\n",
       "        [  9.50593000e-01,   9.90727840e+01],\n",
       "        [  7.18623000e-01,   5.80802560e+01],\n",
       "        [  2.18720000e-01,  -1.96055930e+01],\n",
       "        [  6.64113000e-01,   9.44371590e+01],\n",
       "        [  9.42900000e-01,   1.31725134e+02],\n",
       "        [  3.14226000e-01,   1.89048710e+01],\n",
       "        [  2.84509000e-01,   1.17793460e+01],\n",
       "        [  4.96200000e-03,  -1.46241760e+01],\n",
       "        [  2.24087000e-01,  -5.05476490e+01],\n",
       "        [  9.74331000e-01,   1.12822725e+02],\n",
       "        [  8.94610000e-01,   1.12863995e+02],\n",
       "        [  1.67350000e-01,   7.33800000e-02],\n",
       "        [  7.53644000e-01,   1.05024456e+02],\n",
       "        [  6.32241000e-01,   1.08625812e+02],\n",
       "        [  3.14189000e-01,  -6.09079700e+00],\n",
       "        [  9.65527000e-01,   8.74183430e+01],\n",
       "        [  8.20919000e-01,   9.46105380e+01],\n",
       "        [  1.44107000e-01,  -4.74838700e+00],\n",
       "        [  7.25560000e-02,  -5.68200800e+00],\n",
       "        [  2.44700000e-03,   2.96857140e+01],\n",
       "        [  8.51007000e-01,   7.96323760e+01],\n",
       "        [  4.58024000e-01,  -1.23260260e+01],\n",
       "        [  6.27503000e-01,   1.39458881e+02],\n",
       "        [  4.22259000e-01,  -2.98274050e+01],\n",
       "        [  7.14659000e-01,   6.34802710e+01],\n",
       "        [  6.72320000e-01,   9.36085540e+01],\n",
       "        [  4.98592000e-01,   3.71129750e+01],\n",
       "        [  6.98906000e-01,   9.62828450e+01],\n",
       "        [  8.61441000e-01,   9.96992300e+01],\n",
       "        [  1.12425000e-01,  -1.24199090e+01],\n",
       "        [  1.64784000e-01,   5.24470400e+00],\n",
       "        [  4.81531000e-01,  -1.80704970e+01],\n",
       "        [  3.75482000e-01,   1.77941100e+00],\n",
       "        [  8.93250000e-02,  -1.42167550e+01],\n",
       "        [  3.66090000e-02,  -6.26437200e+00],\n",
       "        [  9.45004000e-01,   5.47235630e+01],\n",
       "        [  1.36608000e-01,   1.49709360e+01],\n",
       "        [  2.92285000e-01,  -4.17237110e+01],\n",
       "        [  2.91950000e-02,  -6.60279000e-01],\n",
       "        [  9.98307000e-01,   1.00124230e+02],\n",
       "        [  3.03928000e-01,  -5.49226400e+00],\n",
       "        [  9.57863000e-01,   1.17824392e+02],\n",
       "        [  8.15089000e-01,   1.13377704e+02],\n",
       "        [  4.66399000e-01,  -1.02498740e+01],\n",
       "        [  8.76693000e-01,   1.15617275e+02],\n",
       "        [  5.36121000e-01,   1.02997087e+02],\n",
       "        [  3.73984000e-01,  -3.73599360e+01],\n",
       "        [  5.65162000e-01,   7.49674760e+01],\n",
       "        [  8.54120000e-02,  -2.14495630e+01],\n",
       "        [  6.86411000e-01,   6.48596200e+01],\n",
       "        [  9.08752000e-01,   1.07983366e+02],\n",
       "        [  9.82829000e-01,   9.80054240e+01],\n",
       "        [  5.27660000e-02,  -4.21395020e+01],\n",
       "        [  7.77552000e-01,   9.18993400e+01],\n",
       "        [  3.74316000e-01,  -3.52250100e+00],\n",
       "        [  6.02310000e-02,   1.00082270e+01],\n",
       "        [  5.26225000e-01,   8.73177220e+01],\n",
       "        [  5.83872000e-01,   6.71044330e+01],\n",
       "        [  2.38276000e-01,   1.06151590e+01],\n",
       "        [  6.78747000e-01,   6.06242730e+01],\n",
       "        [  6.76490000e-02,   1.59473980e+01],\n",
       "        [  5.30182000e-01,   1.05030933e+02],\n",
       "        [  8.69389000e-01,   1.04969996e+02],\n",
       "        [  6.98410000e-01,   7.54604170e+01],\n",
       "        [  5.49430000e-01,   8.25580680e+01]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myDatTest = loadDataSet('ex2test.txt')\n",
    "myMat2Test = mat(myDatTest)\n",
    "myMat2Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n",
      "merging\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'left': {'left': {'left': {'left': 92.523991499999994,\n",
       "    'right': {'left': {'left': {'left': 112.386764,\n",
       "       'right': 123.559747,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.960398},\n",
       "      'right': 135.83701300000001,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.958512},\n",
       "     'right': 111.2013225,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.956951},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.965969},\n",
       "   'right': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 96.41885225,\n",
       "              'right': 69.318648999999994,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.948822},\n",
       "             'right': {'left': {'left': 110.03503850000001,\n",
       "               'right': {'left': 65.548417999999998,\n",
       "                'right': {'left': 115.75399400000001,\n",
       "                 'right': {'left': {'left': 94.396114499999996,\n",
       "                   'right': 85.005351000000005,\n",
       "                   'spInd': 0,\n",
       "                   'spVal': 0.912161},\n",
       "                  'right': {'left': {'left': 106.814667,\n",
       "                    'right': 118.513475,\n",
       "                    'spInd': 0,\n",
       "                    'spVal': 0.908629},\n",
       "                   'right': {'left': 87.300624999999997,\n",
       "                    'right': {'left': {'left': 100.133819,\n",
       "                      'right': 108.09493399999999,\n",
       "                      'spInd': 0,\n",
       "                      'spVal': 0.900699},\n",
       "                     'right': {'left': 82.436685999999995,\n",
       "                      'right': {'left': 98.544549499999988,\n",
       "                       'right': 106.16859550000001,\n",
       "                       'spInd': 0,\n",
       "                       'spVal': 0.872199},\n",
       "                      'spInd': 0,\n",
       "                      'spVal': 0.888426},\n",
       "                     'spInd': 0,\n",
       "                     'spVal': 0.892999},\n",
       "                    'spInd': 0,\n",
       "                    'spVal': 0.901421},\n",
       "                   'spInd': 0,\n",
       "                   'spVal': 0.901444},\n",
       "                  'spInd': 0,\n",
       "                  'spVal': 0.910975},\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.925782},\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.934853},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.936524},\n",
       "              'right': {'left': {'left': 89.20993,\n",
       "                'right': 76.240983999999997,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.847219},\n",
       "               'right': 95.893130999999997,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.84294},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.85497},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.944221},\n",
       "            'right': 60.552307999999996,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.841625},\n",
       "           'right': 124.87935300000001,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.841547},\n",
       "          'right': {'left': 76.723834999999994,\n",
       "           'right': {'left': 59.342323,\n",
       "            'right': 70.054507999999998,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.819722},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.823848},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.833026},\n",
       "         'right': {'left': 118.319942,\n",
       "          'right': {'left': 99.841379000000003,\n",
       "           'right': 112.981216,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.811363},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.811602},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.815215},\n",
       "        'right': 73.494399250000001,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.806158},\n",
       "       'right': {'left': 114.4008695,\n",
       "        'right': 102.26514075,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.786865},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.790312},\n",
       "      'right': 64.041940999999994,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.769043},\n",
       "     'right': 115.199195,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.763328},\n",
       "    'right': 78.085643250000004,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.759504},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.952833},\n",
       "  'right': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 110.90282999999999,\n",
       "         'right': {'left': 103.345308,\n",
       "          'right': 108.55391899999999,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.710234},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.716211},\n",
       "        'right': 135.41676699999999,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.70889},\n",
       "       'right': {'left': {'left': {'left': {'left': 106.18042699999999,\n",
       "           'right': 105.062147,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.70639},\n",
       "          'right': 115.58660500000001,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.699873},\n",
       "         'right': 92.470635999999999,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.69892},\n",
       "        'right': {'left': 120.521925,\n",
       "         'right': {'left': 101.91115275,\n",
       "          'right': 112.78136649999999,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.666452},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.689099},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.698472},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.706961},\n",
       "      'right': {'left': 121.98060700000001,\n",
       "       'right': {'left': 115.687524,\n",
       "        'right': 112.715799,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.652462},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.661073},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.665329},\n",
       "     'right': 82.500765999999999,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.642707},\n",
       "    'right': 140.61394100000001,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.642373},\n",
       "   'right': {'left': {'left': {'left': {'left': 82.713621000000003,\n",
       "       'right': {'left': 91.656616999999997,\n",
       "        'right': 93.645292999999995,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.632691},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.637999},\n",
       "      'right': {'left': 117.62834599999999,\n",
       "       'right': 105.970743,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.624827},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.628061},\n",
       "     'right': 82.04976400000001,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.623909},\n",
       "    'right': {'left': 168.180746,\n",
       "     'right': {'left': {'left': {'left': {'left': {'left': {'left': 93.521395999999996,\n",
       "           'right': {'left': 130.37852899999999,\n",
       "            'right': {'left': 111.9849935,\n",
       "             'right': {'left': 82.589327999999995,\n",
       "              'right': {'left': 114.872056,\n",
       "               'right': 108.43539199999999,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.569327},\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.571214},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.582311},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.589806},\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.599142},\n",
       "          'right': 82.903944999999993,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.560301},\n",
       "         'right': 129.06244849999999,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.553797},\n",
       "        'right': {'left': 83.114502000000002,\n",
       "         'right': {'left': 97.340526499999996,\n",
       "          'right': 90.995536000000001,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.537834},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.546601},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.548539},\n",
       "       'right': {'left': {'left': 129.76674299999999,\n",
       "         'right': 124.795495,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.531944},\n",
       "        'right': 116.17616200000001,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.51915},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.533511},\n",
       "      'right': {'left': 101.075609,\n",
       "       'right': {'left': 93.292828999999998,\n",
       "        'right': 96.403373000000002,\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.508542},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.508548},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.513332},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.606417},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.613004},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.640515},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.729397},\n",
       " 'right': {'left': {'left': {'left': {'left': {'left': 8.5367700000000006,\n",
       "      'right': 27.729263,\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.487381},\n",
       "     'right': 5.224234,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.483803},\n",
       "    'right': {'left': -9.7129250000000003,\n",
       "     'right': -23.777531,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.46568},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.467383},\n",
       "   'right': {'left': 30.051931,\n",
       "    'right': 17.171057000000001,\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.463241},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.465561},\n",
       "  'right': {'left': -34.044555000000003,\n",
       "   'right': {'left': {'left': {'left': {'left': {'left': -4.1911744999999998,\n",
       "        'right': {'left': {'left': {'left': {'left': 19.745224,\n",
       "            'right': 15.224266,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.428582},\n",
       "           'right': -21.594268,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.426711},\n",
       "          'right': 44.161493,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.418943},\n",
       "         'right': {'left': -26.419288999999999,\n",
       "          'right': 0.63593000000000011,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.403228},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.412516},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.437652},\n",
       "       'right': 23.197474,\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.388789},\n",
       "      'right': {'left': {'left': {'left': -29.007783,\n",
       "         'right': {'left': {'left': 13.583555,\n",
       "           'right': 5.2411960000000004,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.377383},\n",
       "          'right': -8.2282969999999995,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.373501},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.378965},\n",
       "        'right': {'left': -32.124495000000003,\n",
       "         'right': {'left': -9.9938275000000001,\n",
       "          'right': -26.851234812500003,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.350725},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.35679},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.370042},\n",
       "       'right': {'left': 22.286959625000001,\n",
       "        'right': {'left': {'left': -20.397333499999998,\n",
       "          'right': -49.939515999999998,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.310956},\n",
       "         'right': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': {'left': 8.8147249999999993,\n",
       "                   'right': {'left': -18.051317999999998,\n",
       "                    'right': {'left': -1.7983769999999999,\n",
       "                     'right': {'left': -14.988279,\n",
       "                      'right': -14.391613,\n",
       "                      'spInd': 0,\n",
       "                      'spVal': 0.290749},\n",
       "                     'spInd': 0,\n",
       "                     'spVal': 0.295993},\n",
       "                    'spInd': 0,\n",
       "                    'spVal': 0.297107},\n",
       "                   'spInd': 0,\n",
       "                   'spVal': 0.300318},\n",
       "                  'right': {'left': 35.623745999999997,\n",
       "                   'right': {'left': -9.4575560000000003,\n",
       "                    'right': {'left': 5.2805790000000004,\n",
       "                     'right': 2.5579230000000002,\n",
       "                     'spInd': 0,\n",
       "                     'spVal': 0.264639},\n",
       "                    'spInd': 0,\n",
       "                    'spVal': 0.264926},\n",
       "                   'spInd': 0,\n",
       "                   'spVal': 0.273863},\n",
       "                  'spInd': 0,\n",
       "                  'spVal': 0.284794},\n",
       "                 'right': {'left': {'left': -9.601409499999999,\n",
       "                   'right': -30.812912000000001,\n",
       "                   'spInd': 0,\n",
       "                   'spVal': 0.228751},\n",
       "                  'right': -2.266273,\n",
       "                  'spInd': 0,\n",
       "                  'spVal': 0.228628},\n",
       "                 'spInd': 0,\n",
       "                 'spVal': 0.25807},\n",
       "                'right': 6.0992389999999999,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.228473},\n",
       "               'right': {'left': -16.427370249999999,\n",
       "                'right': -2.6781804999999999,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.202161},\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.211633},\n",
       "              'right': 9.5773855000000001,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.193282},\n",
       "             'right': {'left': {'left': {'left': -14.740059,\n",
       "                'right': -6.5125060000000001,\n",
       "                'spInd': 0,\n",
       "                'spVal': 0.166431},\n",
       "               'right': -27.405211000000001,\n",
       "               'spInd': 0,\n",
       "               'spVal': 0.164134},\n",
       "              'right': 0.225886,\n",
       "              'spInd': 0,\n",
       "              'spVal': 0.156273},\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.166765},\n",
       "            'right': {'left': 7.5573490000000003,\n",
       "             'right': 7.3367839999999998,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.13988},\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.156067},\n",
       "           'right': -29.087463,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.138619},\n",
       "          'right': 22.478290999999999,\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.131833},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.309133},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.324274},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.335182},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.382037},\n",
       "     'right': -39.524461000000002,\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.130626},\n",
       "    'right': {'left': 22.891674999999999,\n",
       "     'right': {'left': {'left': 6.1965159999999999,\n",
       "       'right': {'left': -16.106164,\n",
       "        'right': {'left': -1.2931950000000001,\n",
       "         'right': -10.137104000000001,\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.085873},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.10796},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.108801},\n",
       "      'right': {'left': 37.820658999999999,\n",
       "       'right': {'left': -24.132225999999999,\n",
       "        'right': {'left': 15.824970500000001,\n",
       "         'right': {'left': -15.160836,\n",
       "          'right': {'left': {'left': {'left': 6.6955669999999996,\n",
       "             'right': -3.131497,\n",
       "             'spInd': 0,\n",
       "             'spVal': 0.055862},\n",
       "            'right': -13.731698,\n",
       "            'spInd': 0,\n",
       "            'spVal': 0.053764},\n",
       "           'right': 4.0916259999999998,\n",
       "           'spInd': 0,\n",
       "           'spVal': 0.044737},\n",
       "          'spInd': 0,\n",
       "          'spVal': 0.061219},\n",
       "         'spInd': 0,\n",
       "         'spVal': 0.068373},\n",
       "        'spInd': 0,\n",
       "        'spVal': 0.080061},\n",
       "       'spInd': 0,\n",
       "       'spVal': 0.084661},\n",
       "      'spInd': 0,\n",
       "      'spVal': 0.085111},\n",
       "     'spInd': 0,\n",
       "     'spVal': 0.124723},\n",
       "    'spInd': 0,\n",
       "    'spVal': 0.126833},\n",
       "   'spInd': 0,\n",
       "   'spVal': 0.455761},\n",
       "  'spInd': 0,\n",
       "  'spVal': 0.457563},\n",
       " 'spInd': 0,\n",
       " 'spVal': 0.499171}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prune(myTree, myMat2Test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'left': matrix([[  1.69855694e-03],\n",
       "         [  1.19647739e+01]]), 'right': matrix([[ 3.46877936],\n",
       "         [ 1.18521743]]), 'spInd': 0, 'spVal': 0.285477}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myMat2 = mat(loadDataSet('exp2.txt'))\n",
    "createTree(myMat2, modelLeaf, modelErr, (1, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "def regTreeEval(model, inDat):\n",
    "    return float(model)\n",
    "\n",
    "def modelTreeEval(model, inDat):\n",
    "    n = shape(inDat)[1]\n",
    "    X = mat(ones((1,n+1)))\n",
    "    X[:,1:n+1]=inDat\n",
    "    return float(X*model)\n",
    "\n",
    "def treeForeCast(tree, inData, modelEval=regTreeEval):\n",
    "    if not isTree(tree): return modelEval(tree, inData)\n",
    "    if inData[tree['spInd']] > tree['spVal']:\n",
    "        if isTree(tree['left']): return treeForeCast(tree['left'], inData, modelEval)\n",
    "        else: return modelEval(tree['left'], inData)\n",
    "    else:\n",
    "        if isTree(tree['right']): return treeForeCast(tree['right'], inData, modelEval)\n",
    "        else: return modelEval(tree['right'], inData)\n",
    "        \n",
    "def createForeCast(tree, testData, modelEval=regTreeEval):\n",
    "    m=len(testData)\n",
    "    yHat = mat(zeros((m,1)))\n",
    "    for i in range(m):\n",
    "        yHat[i,0] = treeForeCast(tree, mat(testData[i]), modelEval)\n",
    "    return yHat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.96408523182221462"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainMat = mat(loadDataSet('bikeSpeedVsIq_train.txt'))\n",
    "testMat = mat(loadDataSet('bikeSpeedVsIq_test.txt'))\n",
    "myTree = createTree(trainMat, ops=(1,20))\n",
    "yHat = createForeCast(myTree, testMat[:, 0])\n",
    "corrcoef(yHat, testMat[:, 1], rowvar=0)[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 37.58916794],\n",
       "        [  6.18978355]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws, X, Y = linearSolve(trainMat)\n",
    "ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.94346842356747618"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(shape(testMat)[0]):\n",
    "    yHat[i] = testMat[i, 0] + ws[0, 0]\n",
    "    \n",
    "corrcoef(yHat, testMat[:, 1], rowvar=0)[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tkinter import *\n",
    "root = Tk()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "myLabel = Label(root, text=\"Hello World\")\n",
    "myLabel.grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "\n",
    "from tkinter import *\n",
    "import regTrees\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.use('TkAgg')\n",
    "from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg\n",
    "from matplotlib.figure import Figure\n",
    "\n",
    "def reDraw(tolS,tolN):\n",
    "    reDraw.f.clf()        # clear the figure\n",
    "    reDraw.a = reDraw.f.add_subplot(111)\n",
    "    if chkBtnVar.get():\n",
    "        if tolN < 2: tolN = 2\n",
    "        myTree=regTrees.createTree(reDraw.rawDat, regTrees.modelLeaf,\\\n",
    "                                   regTrees.modelErr, (tolS,tolN))\n",
    "        yHat = regTrees.createForeCast(myTree, reDraw.testDat, \\\n",
    "                                       regTrees.modelTreeEval)\n",
    "    else:\n",
    "        myTree=regTrees.createTree(reDraw.rawDat, ops=(tolS,tolN))\n",
    "        yHat = regTrees.createForeCast(myTree, reDraw.testDat)\n",
    "    reDraw.a.scatter(reDraw.rawDat[:,0].flatten().A[0], reDraw.rawDat[:,1].flatten().A[0], s=5) #use scatter for data set\n",
    "    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat\n",
    "    reDraw.canvas.show()\n",
    "    \n",
    "def getInputs():\n",
    "    try: tolN = int(tolNentry.get())\n",
    "    except: \n",
    "        tolN = 10 \n",
    "        print(\"enter Integer for tolN\")\n",
    "        tolNentry.delete(0, END)\n",
    "        tolNentry.insert(0,'10')\n",
    "    try: tolS = float(tolSentry.get())\n",
    "    except: \n",
    "        tolS = 1.0 \n",
    "        print(\"enter Float for tolS\")\n",
    "        tolSentry.delete(0, END)\n",
    "        tolSentry.insert(0,'1.0')\n",
    "    return tolN,tolS\n",
    "\n",
    "def drawNewTree():\n",
    "    tolN,tolS = getInputs()#get values from Entry boxes\n",
    "    reDraw(tolS,tolN)\n",
    "    \n",
    "root=Tk()\n",
    "\n",
    "reDraw.f = Figure(figsize=(5,4), dpi=100) #create canvas\n",
    "reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)\n",
    "reDraw.canvas.show()\n",
    "reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)\n",
    "\n",
    "Label(root, text=\"tolN\").grid(row=1, column=0)\n",
    "tolNentry = Entry(root)\n",
    "tolNentry.grid(row=1, column=1)\n",
    "tolNentry.insert(0,'10')\n",
    "Label(root, text=\"tolS\").grid(row=2, column=0)\n",
    "tolSentry = Entry(root)\n",
    "tolSentry.grid(row=2, column=1)\n",
    "tolSentry.insert(0,'1.0')\n",
    "Button(root, text=\"ReDraw\", command=drawNewTree).grid(row=1, column=2, rowspan=3)\n",
    "chkBtnVar = IntVar()\n",
    "chkBtn = Checkbutton(root, text=\"Model Tree\", variable = chkBtnVar)\n",
    "chkBtn.grid(row=3, column=0, columnspan=2)\n",
    "\n",
    "reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt'))\n",
    "reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)\n",
    "reDraw(1.0, 10)\n",
    "               \n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
